<template>
  <div class="XWBBCCGGTZ Editor" >
    <el-dialog :title="dialogTitle" :visible="dialogVisible" :gutter="24" @close="close" width="60%">
      <el-form :gutter="24" :model="param" :rules="rules" ref="reportMistakeInfoActionEditor" :inline="true"  label-width="130px" class="demo-ruleForm">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="报表时间：" prop="reprotTime">
              <el-date-picker type="date" placeholder="报表时间" v-model="param.reprotTime" style="width: 100%;" :disabled="showType==-1" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="reportNameId" label="报表名称：">
              <el-select v-model="param.reportNameId" placeholder="报表名称" :disabled="showType==-1">
                <el-option
                  v-for="(item,index) in reportNameIds"
                  :key="index"
                  :label="item.reportName"
                  :value="item.id+''">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="值班站长：" prop="dutyStationLeaderId">
              <el-select v-model="param.dutyStationLeaderId" placeholder="值班站长" :disabled="showType==-1" @change="(v)=>getUserCode( v ,dutyStationLeaderIds,'dutyStationLeaderCode')" filterable>
                <el-option
                  v-for="(item,index) in dutyStationLeaderIds"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="值班员：" prop="dutyStationPersonId">
              <el-select v-model="param.dutyStationPersonId" placeholder="值班员" :disabled="showType==-1" @change="(v)=>getUserCode(v,dutyStationPersonIds,'dutyStationPersonCode')" filterable>
                <el-option
                  v-for="(item,index) in dutyStationPersonIds"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="站务员：" prop="stationPersonId">
              <el-select v-model="param.stationPersonId" placeholder="站务员" :disabled="showType==-1" @change="(v)=>getUserCode(v,stationPersonIds,'stationpPersonCode')" filterable>
                <el-option
                  v-for="(item,index) in stationPersonIds"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="更改日期：" prop="modifyTime">
              <el-date-picker type="date" placeholder="更改日期" v-model="param.modifyTime" style="width: 100%;" :disabled="showType==-1" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="结算开始日期：" prop="settleBeginTime">
              <el-date-picker type="date" placeholder="结算开始日期" v-model="param.settleBeginTime" style="width: 100%;" :disabled="true" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算结束日期：" prop="settleEndTime">
              <el-date-picker type="date" placeholder="结算结束日期" v-model="param.settleEndTime" style="width: 100%;" :disabled="true" value-format="yyyy-MM-dd"></el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="错误详情：" prop="errorDetail" style="width: 90%">
              <el-input style="width: 400px" type="textarea" v-model="param.errorDetail" :disabled="showType==-1"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <div>
            <el-button type="danger" :disabled="showType==-1" @click="del()" size="mini" style="float: right;margin-left: 10px">删除</el-button>
            <el-button type="primary" :disabled="showType==-1" @click="add()" size="mini" style="float: right;">新增</el-button>
          </div>
          <avue-crud
            class="Editor list"
            ref="crud"
            :option="optionList"
            @selection-change="selectionChange"
            :data="tableData"
      >
            <template slot-scope="scope" slot="stationPersonrId">
              <el-select v-model="scope.row.stationPersonrId" placeholder="请选择" style="width: 150px" @change="changeStationPersonrId(scope.row,$event)" filterable>
                <el-option
                  v-for="(item,index) in sheJiPersons"
                  :key="index"
                  :label="item.nikename"
                  :value="item.userId">
                </el-option>
              </el-select>
            </template>
            <template slot-scope="scope" slot="stationPersonrCode">
             {{scope.row.stationPersonrCode}}
            </template>
          </avue-crud>
        </el-row>
        <br/>
        <br/>
        <br/>
        <el-row :gutter="24" style="text-align: center">
            <el-form-item>
              <el-button type="primary" @click="handleSubmit()" v-if="showType==1">保存</el-button>
              <el-button @click="close()" style="margin-left: 30px" >返回</el-button>
            </el-form-item>
        </el-row>
      </el-form>
    </el-dialog>
  </div>
</template>


<script>
    import {getForm, postForm} from '@/api/gen/gen'
    import moment from "moment";
    import {getPersonRole, getSwPmReportPath} from "../../../../../api/subwayRat/commonCheck";
    import {
      get_xw_SwStReportErrorAlterAppayDetail,
      optionList, update_xw_ReportError,

    } from "../../../../../api/stationTiecketManager/xianWngCJXXQR/BBCCGG/XWBBCCGGTZ/KZDLCK";

    export default {
        data() {
            return {
              showType:0,//0添加，1编辑，-1预览
              dialogTitle:'', //弹框标题
              dialogVisible:false,
              optionList:optionList(),
              tableData:[],
              reportNameIds:[],
              dutyStationLeaderIds:[],
              dutyStationPersonIds:[],
              stationPersonIds:[],
              sheJiPersons:[], //涉及人员
              multipleSelectionRows:[],
              multipleSelectionKeys:[],
              param:{
                id:'',
                lineCode:"",
                stationCode:'',
                reprotTime:"",
                reportNameId:"",
                dutyStationLeaderId:"",
                dutyStationLeaderCode:"",
                dutyStationPersonId:"",
                dutyStationPersonCode:"",
                stationPersonId:"",
                stationpPersonCode:"",
                settleBeginTime:"",
                settleEndTime:"",
                errorDetail:''
              },
              tmp:{
                stationPersonrId:''
              },
              rules: {
                dutyStationLeaderId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                dutyStationPersonId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                stationPersonId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                lineCode: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                stationCode: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                reprotTime: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                reportNameId: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                settleBeginTime: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                settleEndTime: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
                errorDetail: [
                  { required: true, message: '非空', trigger: 'blur' }
                ],
              }
            }
        },
        created() {

        },
        methods: {
          moment,
          add(){
            this.tableData.push({$cellEdit:true});
          },
          del(){
            for(let i=0;i<this.multipleSelectionRows.length;i++){
              this.tableData.splice(this.multipleSelectionRows[i].$index,1);
            }
          },
          handleSubmit() {
            // debugger
            // this.$refs.reportMistakeInfoActionEditor.validate((valid) => {
            //   if (valid) {
            //     this.param.userCode = this.$store.getters.userInfo.userCode;
            let row=JSON.stringify(this.param);
            let datas=JSON.stringify(this.tableData);
            update_xw_ReportError({row,row,sheJiPersons:datas}).then((res)=>{
              this.$parent.search();
                  this.close();
                  this.$message({
                    message: res.data.code==0?'操作成功':'操作失败',
                    type: res.data.code==0?'success':'error',
                  });
                })
          },
          previewEditor(row,showType,title){
            this.param.dutyStationLeaderId = row.dutyStationLeaderId;
            this.param.dutyStationPersonId = row.dutyStationPersonId;
            this.param.stationPersonId = row.stationPersonId;
            this.dialogTitle=title;
            this.showType=showType;
            this.dialogVisible=true;
            Object.assign(this.param,row);

            if(showType==-1 || showType==1){ //预览，编辑
              this.getSheJIPerson(row);
              this.getPersonRoles(row.lineCode,row.stationCode);
            }else if(showType==0){
              let line_code=this.$store.getters.userInfo.lineCode;
              let station_code = this.$store.getters.userInfo.stationCode;
              this.tableData=[];
              this.getPersonRoles(line_code,station_code);
            }

          },
          getPersonRoles(line_code,station_code){
              debugger
            this.sheJiPersons=[];
            getPersonRole({roleCode:'ZBZZ',lineCode:line_code,stationCode:station_code}).then((res)=>{
              this.dutyStationLeaderIds=res.data.data;
              this.sheJiPersons=this.sheJiPersons.concat(this.dutyStationLeaderIds);
            });
            getPersonRole({roleCode:'KYZBY',lineCode:line_code,stationCode:station_code}).then((res)=>{
              this.dutyStationPersonIds=res.data.data;
              this.sheJiPersons=this.sheJiPersons.concat(this.dutyStationPersonIds);
            });
            getPersonRole({roleCode:'ZHANWURENYUAN',lineCode:line_code,stationCode:station_code}).then((res)=>{
              this.stationPersonIds=res.data.data;
              this.sheJiPersons=this.sheJiPersons.concat(this.stationPersonIds);
            });
          },
          initReportNameIds(){ //初始化报表名
            getSwPmReportPath().then(res=>{
              this.reportNameIds=res.data.data;
            })
          },
          getUserCode(id,users,codeVar){
            console.log(users,id,codeVar);
            for(let i=0;i<users.length;i++){
              if(users[i].userId==id){
                this.param[codeVar]=users[i].userCode;
              }
            }
          },
          getSheJIPerson(row){ //涉及人员
            //获取涉及人员
            get_xw_SwStReportErrorAlterAppayDetail({relationid:row.id}).then(res=>{
              this.tableData=res.data.data;
            })
          },
          changeStationPersonrId(row,id){
            for(let i=0;i<this.sheJiPersons.length;i++){
              if(this.sheJiPersons[i].userId==id){
                row.stationPersonrName=this.sheJiPersons[i].username;
                row.stationPersonrCode=this.sheJiPersons[i].userCode;
                return;
              }
            }

          },
          close(){
            this.dialogVisible=false;
          },
          clearParam(){
            this.param={
              id:'',
              lineCode:"",
              stationCode:'',
              reprotTime:"",
              reportNameId:"",
              dutyStationLeaderId:"",
              dutyStationLeaderCode:"",
              dutyStationPersonId:"",
              dutyStationPersonCode:"",
              stationPersonId:"",
              stationpPersonCode:"",
              settleBeginTime:"",
              settleEndTime:"",
              errorDetail:'',
              cardNo:'',
            };
          },
          selectionChange(list){
            this.multipleSelectionRows=list;
            this.multipleSelectionKeys=[];
            for(let i=0;i<list.length;i++){
              this.multipleSelectionKeys.push(list[i].id);
            }
          },
        },
      mounted() {
          this.initReportNameIds();
      }
    }
</script>
<style >
  .Editor.list .avue-crud__tip.el-tag{
        display: none;
  }
</style>
